﻿using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ZoneLineNew.Models.Entity;
using ZoneLineNew.Services.DB;
using ZoneLineNew.Services.Message;
using ZoneLineNew.ViewModelBase;

namespace ZoneLineNew.ViewModels {

    public class TaskWorkOrderViewModel : NavigationViewModel {

        public TaskWorkOrderViewModel(
            IRegionManager regionManager, 
            IDialogService dialog,
            IMessageService messageBox, 
            IEventAggregator eventAggregator, 
            IDbOperate<IFreeSqlMainContext> dbOperate) 
            : base(regionManager, dialog, messageBox, eventAggregator, dbOperate) {

        }


        #region Props

        private string _searchText;

        public string SearchText {
            get { return _searchText; }
            set { _searchText = value; base.RaisePropertyChanged(); }
        }

        private ObservableCollection<WorkOrderEntity> _workOrders = new ObservableCollection<WorkOrderEntity>();

        public ObservableCollection<WorkOrderEntity> WorkOrders {
            get { return _workOrders; }
            set { _workOrders = value; base.RaisePropertyChanged(); }
        }


        private WorkOrderEntity _selectWorkOrder;

        public WorkOrderEntity SelectWorkOrder {
            get { return _selectWorkOrder; }
            set { _selectWorkOrder = value; base.RaisePropertyChanged(); }
        }
        #endregion


        #region Commands
        public DelegateCommand SearchCommand => new DelegateCommand(() => {
            //项目名称、工单号、物料名称、物料代码
            var list = _mainDbOperate.GetFreeSql()
            .Select<WorkOrderEntity>()
            .WhereIf(!string.IsNullOrEmpty(SearchText),
            o => o.ProjectName.Contains(SearchText) ||
            o.WorkOrderNumber.Contains(SearchText) ||
            o.MaterialName.Contains(SearchText) ||
            o.MaterialCode.Contains(SearchText)).ToList();
            WorkOrders.Clear();
            WorkOrders.AddRange(list);
        });

        public DelegateCommand AddCommand => new DelegateCommand(() => {

        });


        public DelegateCommand<WorkOrderEntity> DeleteCommand => new DelegateCommand<WorkOrderEntity>((rhs) => {
            if (rhs != null) {
                var res = _message.Ask("此操作不可逆，是否确定【删除】当前工单信息？", "温馨提示");
                if (res == MessageBoxResult.OK) {
                    var rlt = _mainDbOperate.DeleteModel<WorkOrderEntity>(o=>o.Id == rhs.Id);
                    if (rlt) {
                        _message.GrowSuccess("删除成功！");
                    } else {
                        _message.GrowError("操作失败！");
                    }
                    SearchCommand.Execute();
                }
            }
        });
        #endregion



        protected override void OnViewLoaded() {

            SearchCommand.Execute();

            //base.OnViewLoaded();
        }


    }

}
